//
//  WSBmob.m
//  GroupFourFM
//
//  Created by dllo on 15/12/30.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSBmob.h"
#import <BmobSDK/Bmob.h>

@implementation WSBmob

+ (instancetype)shareBmob {

    static WSBmob *bmob = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        bmob = [[WSBmob alloc] init];
    });
    
    return bmob;
}

- (void)collectAuthorWithAuthorId:(NSInteger)id title:(NSString *)title content:(NSString *)content image:(NSString *)image block:(void(^)(id result))block {

    BmobObject  *author = [BmobObject objectWithClassName:@"Collect_author"];

    [author setObject:[NSNumber numberWithInteger:id] forKey:@"authorId"];

    [author setObject:title forKey:@"authorName"];
    
    [author setObject:content forKey:@"authorContent"];
    
    [author setObject:image forKey:@"headImage"];
    
    //异步保存到服务器
    [author saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            
            //获取要添加关联关系的post
            BmobObject *post = [BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[[NSUserDefaults standardUserDefaults] objectForKey:@"uId"]];
            
            //新建relation对象
            BmobRelation *relation = [[BmobRelation alloc] init];
            [relation addObject:[BmobObject objectWithoutDatatWithClassName:@"Collect_author" objectId:author.objectId]];
            
            //添加关联关系到likes列中
            [post addRelation:relation forKey:@"likes"];
            //异步更新obj的数据
            [post updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    block(author.objectId);
                }else{
                    block(nil);
                }
            }];
        } else if (error){
            //发生错误后的动作
            block(@"fail");
        } else {
            block(@"fail");
        }
    }];
    
}

- (void)selectRelationWithUid:(NSString *)uid tableName:(NSString *)tableName block:(myblock)block{

    //关联对象表
    BmobQuery *bquery = [BmobQuery queryWithClassName:tableName];
    
    //需要查询的列
    BmobObject *post = [BmobObject objectWithoutDatatWithClassName:@"_User" objectId:uid];
    [bquery whereObjectKey:@"likes" relatedTo:post];
    
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (error) {
            NSLog(@"%@",error);
        } else {
            block(array);
        }
    }];
}

/**
 *  删除关联关系
 *
 *  @param objectId  用户id
 *  @param tableName 删除表名
 *  @param block     是否成功
 */
- (void)deleteRelationWithObjectId:(NSString *)objectId tableName:(NSString *)tableName block:(myblock)block {

    BmobObject *post = [BmobObject objectWithoutDatatWithClassName:@"_User" objectId:[[NSUserDefaults standardUserDefaults] objectForKey:@"uId"]];
    
    //新建relation对象
    BmobRelation *relation = [[BmobRelation alloc] init];
    [relation removeObject:[BmobObject objectWithoutDatatWithClassName:tableName objectId:objectId]];
    
    //添加关联关系到likes列中
    [post addRelation:relation forKey:@"likes"];
    
    //异步更新obj的数据
    [post updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        
        if (isSuccessful) {
            BmobObject *bmobObject = [BmobObject objectWithoutDatatWithClassName:tableName  objectId:objectId];
            [bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
                if (isSuccessful) {
                    //删除成功后的动作
                    block(@"successful");
                } else if (error){
                    block(@"fail");
                    NSLog(@"%@",error);
                } else {
                    block(@"fail");
                    NSLog(@"UnKnow error");
                }
            }];
        }else{
            block(@"fail");
            NSLog(@"error %@",[error description]);
        }
    }];
}

- (void)selectInfoWithTableName:(NSString *)tableName rowName:(NSString *)name value:(NSString *)value block:(myblock)block {

    BmobQuery *bquery = [BmobQuery queryWithClassName:tableName];
    
    [bquery whereKey:name equalTo:value];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        block(array);
    }];
}

@end
